package com.zwf.service.impl;

import com.zwf.constant.StatusConstant;
import com.zwf.context.BaseContext;
import com.zwf.entity.AddressBook;
import com.zwf.mapper.AddressBookMapper;
import com.zwf.result.Result;
import com.zwf.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zwf
 * @date 2024/2/10 19:55
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    AddressBookMapper addressBookMapper;

    /**
     * 新增地址
     * @param addressBook
     */
    @Override
    public void add(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());

        //新增地址规定不为默认地址
        addressBook.setIsDefault(StatusConstant.DISABLE);

        addressBookMapper.add(addressBook);
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @Override
    public List<AddressBook> list() {
        AddressBook addressBook=new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> addressBookList= addressBookMapper.list(addressBook);
        return addressBookList;
    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    public void setDefaultAdd(AddressBook addressBook) {

        //将当前所有用户改为非默认地址
        addressBook.setIsDefault(StatusConstant.DISABLE);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateAllToNotDef(addressBook);

        addressBook.setIsDefault(StatusConstant.ENABLE);
        addressBookMapper.update(addressBook);
    }

    /**
     * 查询默认地址
     * @return
     */
    @Override
    public AddressBook findDefault() {
        AddressBook addressBook=new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(StatusConstant.ENABLE);
        List<AddressBook> list = addressBookMapper.list(addressBook);
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return addressBook;  //没有查到默认地址，将addressBook返回，因其id为null，可用于controller判断是否有默认地址
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook=new AddressBook();
        addressBook.setId(id);
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookMapper.list(addressBook);
        return list.get(0);
    }

    /**
     * 根据id删除地址
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        AddressBook addressBook=new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setId(id);
        addressBookMapper.deleteById(id);
    }

    /**
     * 根据id修改地址
     * @param addressBook
     */
    @Override
    public void modify(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }
}
